package cloud.seri.iot.aliyun

import com.aliyuncs.DefaultAcsClient
import com.aliyuncs.exceptions.ClientException
import com.aliyuncs.iot.model.v20180120.*

class DeviceManager(private val client: DefaultAcsClient)
{

    /**
     * 注册设备
     *
     * @param ProductKey 产品名称  必须
     * @param DeviceName 设备命名  非必须
     * @return 产品创建信息
     */
    @Throws(ClientException::class)
    fun registerDevice(ProductKey: String, DeviceName: String, Nickname: String? = null): RegisterDeviceResponse?
    {

        val request = RegisterDeviceRequest()
        request.deviceName = DeviceName
        request.productKey = ProductKey
        request.nickname = Nickname
        return client.getAcsResponse(request)
    }

    /**
     * 删除设备。
     *
     * @param ProductKey 产品名称  非必须
     * @param DeviceName 设备的名称  非必须
     * @param IotId 设备ID    非必须
     * @return 产品创建信息
     */
    @Throws(ClientException::class)
    fun deleteDevice(IotId: String? = null, ProductKey: String? = null, DeviceName: String? = null): DeleteDeviceResponse?
    {

        val request = DeleteDeviceRequest()
        request.productKey = ProductKey
        request.iotId = IotId
        request.deviceName = DeviceName
        return client.getAcsResponse(request)
    }

    /**
     * 查询指定设备的详细信息
     *
     * @param IotId 设备ID  非必须
     * @param ProductKey 设备所隶属的产品Key。  非必须
     * @param DeviceName 设备名  非必须
     * @return 产品创建信息
     */
    @Throws(ClientException::class)
    fun queryDeviceDetail(IotId: String? = null, ProductKey: String? = null, DeviceName: String? = null): QueryDeviceDetailResponse?
    {

        val request = QueryDeviceDetailRequest()
        request.iotId = IotId
        request.deviceName = DeviceName
        request.productKey = ProductKey
        return client.getAcsResponse(request)
    }

    /**
     * 查询指定产品下的所有设备列表。
     *
     * @param ProductKey 产品名称  必须
     * @param PageSize 设备命名  非必须
     * @param CurrentPage 设备命名  非必须
     * @return 产品创建信息
     */
    @Throws(ClientException::class)
    fun queryDevice(ProductKey: String, PageSize: Int? = null, CurrentPage: Int? = null): QueryDeviceResponse?
    {
        val request = QueryDeviceRequest()
        request.productKey = ProductKey
        request.currentPage = CurrentPage
        request.pageSize = PageSize
        return client.getAcsResponse(request)
    }

    /**
     * 批量注册多个设备
     *
     * @param ProductKey 产品名称  必须
     * @param Count 设备的数量 必须
     * @Des 描述：
     */
    @Throws(ClientException::class)
    fun batchRegisterDevice(ProductKey: String, Count: Int): BatchRegisterDeviceResponse?
    {
        val registerDeviceRequest = BatchRegisterDeviceRequest()
        registerDeviceRequest.count = Count
        registerDeviceRequest.productKey = ProductKey
        return client.getAcsResponse(registerDeviceRequest)
    }

    /**
     * 在指定产品下批量自定义设备名称
     *
     * @param ProductKey 产品名称  非必须
     * @param DeviceNames 设备的名称  非必须
     * 描述：
     * 该接口需要和BatchRegisterDeviceWithApplyId接口结合使用，实现在一个产品下批量注册（即新建）
     * 多个设备，并且为每个设备单独命名。单次调用，最多能传入1,000 个设备名称。
     */
    @Throws(ClientException::class)
    fun batchCheckDeviceNames(ProductKey: String? = null, DeviceNames: List<String>? = null): BatchCheckDeviceNamesResponse?
    {

        val request = BatchCheckDeviceNamesRequest()
        request.deviceNames = DeviceNames
        request.productKey = ProductKey
        return client.getAcsResponse(request)
    }

    /**
     * 根据申请批次ID（ApplyId）批量注册设备
     *
     * @param ProductKey 产品名称  必须
     * @param ApplyId 要批量注册的设备的申请批次ID。申请批次ID由调用BatchCheckDeviceNames接口返回。  必须
     * @Des 描述：
     * 该接口需要和BatchCheckDeviceNames接口结合使用，实现在一个产品下批量注册（即新建）多个设备，并且为每个设备单独命名。
     */
    @Throws(ClientException::class)
    fun batchRegisterDeviceWithApplyId(ProductKey: String, ApplyId: Long): BatchRegisterDeviceWithApplyIdResponse?
    {

        val request = BatchRegisterDeviceWithApplyIdRequest()
        request.applyId = ApplyId
        request.productKey = ProductKey
        return client.getAcsResponse(request)
    }

    /**
     * 查询批量注册设备申请的处理状态和结果
     *
     * @param ProductKey 产品名称  必须
     * @param ApplyId 设备的数量 必须
     * @Des 描述：
     */
    @Throws(ClientException::class)
    fun queryBatchRegisterDeviceStatus(ProductKey: String, ApplyId: Long): QueryBatchRegisterDeviceStatusResponse?
    {
        val request = QueryBatchRegisterDeviceStatusRequest()
        request.applyId = ApplyId
        request.productKey = ProductKey
        return client.getAcsResponse(request)
    }

    /**
     * 查询批量注册的设备信息
     *
     * @param ApplyId 产品名称  必须
     * @param PageSize 必须
     * @param CurrentPage 必须
     * @Des 描述：
     */
    @Throws(ClientException::class)
    fun queryPageByApplyId(ApplyId: Long, PageSize: Int, CurrentPage: Int): QueryPageByApplyIdResponse?
    {
        val request = QueryPageByApplyIdRequest()
        request.applyId = ApplyId
        request.currentPage = CurrentPage
        request.pageSize = PageSize
        return client.getAcsResponse(request)
    }

    /**
     * 查询指定产品下的设备统计数据
     *
     * @param ProductKey 设备所隶属的产品Key
     * @Des 描述：
     */
    @Throws(ClientException::class)
    fun queryDeviceStatistics(ProductKey: String): QueryDeviceStatisticsResponse?
    {
        val request = QueryDeviceStatisticsRequest()
        request.productKey = ProductKey
        return client.getAcsResponse(request)
    }

    /********************************操作设备 */

    /**
     * 查看指定设备的运行状态
     *
     * @param ProductKey 产品名称  非必须
     * @param DeviceName 设备的名称  非必须
     * @param IotId 设备ID    非必须
     * @return 产品创建信息
     */
    @Throws(ClientException::class)
    fun getDeviceStatus(ProductKey: String? = null, DeviceName: String? = null, IotId: String? = null): GetDeviceStatusResponse?
    {

        val request = GetDeviceStatusRequest()
        request.productKey = ProductKey
        request.deviceName = DeviceName
        request.iotId = IotId

        return client.getAcsResponse(request)
    }

    /**
     * 批量查看指定设备的运行状态
     *
     * @param ProductKey 产品名称  非必须
     * @param DeviceNames 设备的名称  非必须
     * @return 产品创建信息
     */
    @Throws(ClientException::class)
    fun batchGetDeviceState(ProductKey: String, DeviceNames: List<String>): BatchGetDeviceStateResponse?
    {

        val request = BatchGetDeviceStateRequest()
        request.deviceNames = DeviceNames
        request.productKey = ProductKey
        return client.getAcsResponse(request)
    }

    /**
     * 禁用指定设备
     *
     * @param IotId 设备的名称  非必须
     * @param ProductKey 产品名称  非必须
     * @param DeviceName 设备的名称  非必须
     * @return 产品创建信息
     */
    @Throws(ClientException::class)
    fun disableThing(IotId: String? = null, ProductKey: String? = null, DeviceName: String? = null): DisableThingResponse?
    {
        val request = DisableThingRequest()
        request.deviceName = DeviceName
        request.iotId = IotId
        request.productKey = ProductKey
        return client.getAcsResponse(request)
    }

    /**
     * 启用指定设备
     *
     * @param IotId 设备的名称  非必须
     * @param ProductKey 产品名称  非必须
     * @param DeviceName 设备的名称  非必须
     * @return 产品创建信息
     */
    @Throws(ClientException::class)
    fun enableThing(IotId: String? = null, ProductKey: String? = null, DeviceName: String? = null): EnableThingResponse?
    {
        val request = EnableThingRequest()
        request.iotId = IotId
        request.deviceName = DeviceName
        request.productKey = ProductKey

        return client.getAcsResponse(request)
    }
}
